<script setup lang="ts">
import { type FormInstance, Message, type TreeNodeData } from '@arco-design/web-vue'
import { EmployeeApi } from '@/apis'
// import type { Gender, Status } from '@/types/global'
import { useForm, useOrg, useRole } from '@/hooks'
import * as Regexp from '@/utils/regexp'
import { generateRandomStr } from '@/utils/common'
// import { useDept, useRole } from '@/hooks'

const emit = defineEmits<{
  (e: 'change'): void
}>()
const { roleList, getRoleList } = useRole()
const { orgTree, getOrgTree } = useOrg()
// const roleList = ref([])

// 过滤部门
const filterOrgOptions = (searchKey: string, nodeData: TreeNodeData) => {
  if (nodeData.title) {
    return nodeData.title.toLowerCase().includes(searchKey.toLowerCase())
  }
  return false
}

const visible = ref(false)
const formRef = ref<FormInstance>()

const rules: FormInstance['rules'] = {
  name: [{ required: true, match: Regexp.Name, message: '姓名只能含有中文、数字、英文、下划线、短横线，长度为2-32位' }],
  code: [{ required: false, match: Regexp.Code, message: '编码只能含有英文、数字、下划线、短横线，以字母开头，长度为2-32位' }],
  mobilePhone: [{ required: true, match: Regexp.Phone, message: '手机号不符合规则' }],
  password: [{ required: true, message: '请输入密码' }],
  // type:[{ required: true, message: '请选择员工类型'}],
  // orgId: [{ required: true, message: '请选择所属部门' }],
  // roleIds: [{ required: true, message: '请选择角色' }]
}

const { form, resetForm } = useForm({
  name: null,
  code: null,
  mobilePhone: null,
  password: generateRandomStr(10),
  gender: 0,
  status: 1,
  type: null,
})

// 重置
const reset = () => {
  formRef.value?.resetFields()
  resetForm()
}

// 新增
const open = () => {
  if (!orgTree.value.length) {
    getOrgTree()
  }
  if (!roleList.value.length) {
    getRoleList()
  }
  reset()
  visible.value = true
}

// 保存
const onSave = async () => {
  const isInvalid = await formRef.value?.validate()
  if (isInvalid) {
    return false
  }
  await EmployeeApi.create(form)
  Message.success('添加成功')
  emit('change')
  return true
}

const onRandomPassword = () => {
  form.password = generateRandomStr(10)
}

defineExpose({ open })
</script>

<template>
  <ProDrawer
    v-model:visible="visible"
    title="添加成员"
    @before-ok="onSave"
    @close="reset"
  >
    <a-form ref="formRef" layout="vertical" :model="form" :rules size="medium">
      <a-card title="基础信息" :bordered="false" class="section">
        <a-grid :cols="2" :col-gap="28">
          <a-form-item label="员工姓名" field="name">
            <a-input v-model.trim="form.name" placeholder="请输入员工姓名" :max-length="32" />
          </a-form-item>
          <a-form-item label="员工编码" field="code" tooltip="员工编号指员工唯一标识，一般为工号等。">
            <a-input v-model.trim="form.code" placeholder="请输入员工编码" :max-length="32" />
          </a-form-item>
          <a-form-item label="手机号码" field="mobilePhone">
            <a-input v-model.trim="form.mobilePhone" placeholder="请输入手机号码" :max-length="11" />
          </a-form-item>
          <a-form-item label="性别" field="gender">
            <DictSelect v-model="form.gender" type="radio" code="GenderEnum" />
          </a-form-item>
          <a-form-item label="员工类型" field="type">
            <DictSelect v-model="form.type" type="select" code="EmployeeTypeEnum" placeholder="员工类型" />
          </a-form-item>
        </a-grid>
      </a-card>
      <a-card title="组织信息" class="section">
        <a-grid :cols="2" :col-gap="28">
          <a-form-item label="所属组织" field="orgId">
            <a-tree-select
              v-model="form.orgId"
              :data="orgTree"
              placeholder="请选择组织"
              allow-clear
              allow-search
              :filter-tree-node="filterOrgOptions"
            >
              <template #empty />
            </a-tree-select>
          </a-form-item>
          <a-form-item label="所属角色" field="roleIds">
            <a-select
              v-model="form.orgIds"
              placeholder="请选择角色"
              allow-clear
              multiple
              :scrollbar="true"
            >
              <a-option v-for="role in roleList" :key="role.id" :value="role.id">
                {{ role.name }}
              </a-option>
            </a-select>
          </a-form-item>
        </a-grid>
      </a-card>
      <a-card title="账户信息" class="section">
        <a-grid :cols="2" :col-gap="28">
          <a-form-item label="初始密码" field="password">
            <a-input-password v-model.trim="form.password" placeholder="请输入初始密码" :max-length="32" autocomplete="on" :default-visibility="false">
              <template #append>
                <span class="hover:cursor-pointer" @click="onRandomPassword">随机生成</span>
              </template>
            </a-input-password>
          </a-form-item>
          <a-form-item label="状态" field="status">
            <DictSelect v-model="form.status" code="CommonStatusEnum" type="switch" />
          </a-form-item>
        </a-grid>
      </a-card>
    </a-form>
  </ProDrawer>
</template>

<style lang="less" scoped>
</style>
